<?php
namespace app\controller\demo;

use app\models\User;
use think\Db;

class Database extends Application
{
    public function getSelect()
    {
        dump(Db::table('user')->find());
        dump(Db::name('user')->find());
        dump(db('user')->find());
    }

    public function getField()
    {
        dump(Db::table('user')->where('id', 1)->value('name'));
    }

    public function getColumn()
    {
        dump(Db::table('user')->column('name'));
    }

    public function getChunk()
    {
        Db::table('mr_car_model')->chunk(200, function ($model) {
            foreach ($model as $m) {
                dump($m['model_name']);
            }
        });
    }

    public function getCursor()
    {
        $cursor = Db::table('mr_car_model')->cursor();
        foreach($cursor as $model){
            dump($model['model_name']);
        }
    }

    public function getInsert()
    {
        $data = ['name' => 'xue', 'email' => '85171648@qq.com'];
        dump(Db::name('user')->insert($data));
        $user = new User;
        $user->name = 'qiqi';
        $user->email = 'qiqi@qq.com';
        dump($user->save());
    }

    public function getUpdate()
    {
        set_time_limit(0);
        for ($i = 1; $i <= 30; $i++) {
            $user = User::where('id', 1)->find();
            $user->name = 'oujian';
            dump($user->lock_version);
            sleep(rand(1,3));
            dump($user->save());
        }
    }
}
